import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/pages/main/main_controller.dart';
import 'package:yysk_app/routes/global_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/input_widget.dart';
import 'package:yysk_app/widget/verification_code_widet.dart';
import 'package:oktoast/oktoast.dart';

//注销账号
Future<void> showLogoutSheet() async {
  final GlobalController globalController = Get.find();
  final MainController controller = Get.find();
  controller.invitedController.clear();
  controller.invitedCode.value = '';
  final pwdErrorOne = ''.obs;

  int? smsId;
  String? codes;
  String? pwd;

  // --- 将 validatePasswordOne 提前声明 ---
  void validatePasswordOne(String password) async {
    // 定义正则表达式，要求密码同时包含数字、字母及特殊字符，且长度至少为8个字符
    RegExp regex = RegExp(r'^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,18}$');
    // 使用正则表达式进行匹配
    if (!regex.hasMatch(password)) {
      // 如果密码不符合规则，返回一个错误消息
      pwdErrorOne.value = '登录密码8-18位数，包含字母和数字，不支持特殊字符';
      return;
    }
    pwdErrorOne.value = '';
  }
  void changeValue(dynamic value, String type) async {
    if (type == 'pwd') {
      pwd = value;
      validatePasswordOne(value);
    } else if (type == 'smsId') {
      smsId = value;
    }
    if (type == 'codes') {
      codes = value;
    }
  }
  // --- end ---

  showModalBottomSheet(
    context: Get.context!,
    isDismissible: false,
    isScrollControlled: true,

    backgroundColor: Color.fromRGBO(0, 0, 0, 0),
    builder: (BuildContext context) {
      return BottomSheet(
        onClosing: () {},
        enableDrag: false, // 禁止拖拽，否则报错
        backgroundColor: Color.fromRGBO(0, 0, 0, 0),
        builder: (BuildContext context) {
          return Container(
            padding: EdgeInsets.fromLTRB(
              0,
              0,
              0,
              MediaQuery.of(context).viewInsets.bottom,
            ),

            // height: MediaQuery.of(context).size.height,
            height:
                500.px +
                (controller.isFocused.value
                    ? MediaQuery.of(context).viewInsets.bottom
                    : 0),
            constraints: BoxConstraints(minHeight: 500.px, maxHeight: 800.px),
            width: MediaQuery.of(context).size.width,

            // clipBehavior: Clip.antiAlias,
            child: Stack(
              children: [
                Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    SizedBox(
                      height: 46.px,
                      width: 375.px,
                      // color: AppColors.backgroundColorLightGreen,
                      child: Stack(
                        children: [
                          Positioned(
                            top: 0,
                            right: 8.px,
                            child: GestureDetector(
                              onTap: () {
                                Navigator.of(context).pop();
                              },
                              child: ImageAssetWidget(
                                imageUrl: 'static/images/close2_btm.png',
                                width: 29.px,
                                height: 29.px,
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: Container(
                        clipBehavior: Clip.antiAlias,
                        decoration: BoxDecoration(
                          color: AppColors.backgroundColorGreyF7,

                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(20.px),
                            topRight: Radius.circular(20.px),
                          ),
                        ),
                        child: ListView(
                          children: [
                             SizedBox(height: 40.px),

                            Text(
                              '注销校验',
                              style: TextStyle(
                                color: AppColors.fontColorBlack,
                                fontSize: 26.px,
                                fontWeight: FontWeight.w500,
                              ),
                              textAlign: TextAlign.center,
                            ),
                            SizedBox(height: 8.px),
                            Text(
                              '注销账号：${globalController.user.value?.userTel??''}',
                              style: TextStyle(
                                color: AppColors.backgroundColorBlack,
                                fontSize: 14.px,
                                fontWeight: FontWeight.w400,
                              ),
                              textAlign: TextAlign.center,
                            ),
                            SizedBox(height: 18.px),
                            Center(
                              child: SizedBox(
                                width: 329.px,
                                // height: 78.px,
                                child: InputWidget(
                                  focusNode: controller.focusNode,
                                  maxLength: 6,
                                  controller: controller.invitedController,
                                  inputType: TextInputType.text,
                                  hint: '请输入登录密码',
                                  backgroundColor: AppColors.backgroundColor,
                                  onChanged: (value) {
                                    controller.invitedCode.value = value;
                                  },
                                ),
                              ),
                            ),
                            SizedBox(height: 8.px),
                            Center(
                          child: Container(
                          width: 329.px,
                          decoration: BoxDecoration(
                          color: AppColors.backgroundColorGreyF7,
                          borderRadius: BorderRadius.all(
                           Radius.circular(8.px)
                          ),
                        ),
                                child: VerificationCodeWidet(
                                            initialCount: 60,
                                            hintText: '请输入验证码',
                                            type: VerificationCodeType.LOGOUT,
                                            phoneNumber:
                                                globalController.user.value?.userTel ?? '',
                                            onChanged: ({String? code, int? msgId}) {
                                              if (code != null && code != '') {
                                                changeValue(code, 'codes');
                                              }
                                              if (msgId != null) {
                                                changeValue(msgId, 'smsId');
                                              }
                                            },
                                            buttonRadio: 8,
                                ),
                              ),
                            ),

                           
                          ],
                        ),
                      ),
                    ),
                    Container(
                      color: AppColors.backgroundColorGreyF7,
                      height: 82.px,
                      child: Center(
                        child: ButtonWidget(
                          label: '确认注销',
                          width: 322.px,
                          height: 54.px,
                          imageBg: 'static/images/btm.png',
                          contentType: ContentType.other,
                          fontSize: 22.px,
                          fontColor: AppColors.backgroundColorLightGreen,
                          onPressed: () {
                            if (pwd == '') {
                              showToast('请输入密码');
                              return;
                            }
                           
                           
                          },
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          );
        },
      );
    },
   
  );

 
}

 